home *** CD-ROM | disk | FTP | other *** search
/ Aminet 45 / Aminet 45 (2001)(GTI - Schatztruhe)[!][Oct 2001].iso / Aminet / gfx / x11 / x3270_3_2_16.lha / amiga_src / print.c < prev    next >
C/C++ Source or Header  |  2008-10-18  |  7KB  |  305 lines

  1. /*
  2.  * Copyright 1994, 1995, 1999, 2000 by Paul Mattes.
  3.  *  Permission to use, copy, modify, and distribute this software and its
  4.  *  documentation for any purpose and without fee is hereby granted,
  5.  *  provided that the above copyright notice appear in all copies and that
  6.  *  both that copyright notice and this permission notice appear in
  7.  *  supporting documentation.
  8.  */
  9.  
  10. /*
  11.  *    print.c
  12.  *        Screen printing functions.
  13.  */
  14.  
  15. #include "globals.h"
  16.  
  17. #include "appres.h"
  18. #include "3270ds.h"
  19. #include "ctlr.h"
  20.  
  21. #include "ctlrc.h"
  22. #include "tablesc.h"
  23.  
  24. #if defined(X3270_DISPLAY) /*[*/
  25. #include <errno.h>
  26. #include <X11/StringDefs.h>
  27. #include <X11/Xaw/Dialog.h>
  28.  
  29. #include "objects.h"
  30. #include "resources.h"
  31.  
  32. #include "actionsc.h"
  33. #include "popupsc.h"
  34. #include "printc.h"
  35. #include "utilc.h"
  36.  
  37. /* Statics */
  38.  
  39. static Widget print_text_shell = (Widget)NULL;
  40. static Widget print_window_shell = (Widget)NULL;
  41. static char *print_window_command = CN;
  42.  
  43. #endif /*]*/
  44.  
  45.  
  46. /* Print Text popup */
  47.  
  48. /*
  49.  * Print the ASCIIfied contents of the screen onto a stream.
  50.  * Returns True if anything printed, False otherwise.
  51.  */
  52. Boolean
  53. fprint_screen(FILE *f, Boolean even_if_empty)
  54. {
  55.     register int i;
  56.     unsigned char e;
  57.     char c;
  58.     int ns = 0;
  59.     int nr = 0;
  60.     Boolean any = False;
  61.     unsigned char fa = *get_field_attribute(0);
  62.  
  63.     for (i = 0; i < ROWS*COLS; i++) {
  64.         if (i && !(i % COLS)) {
  65.             nr++;
  66.             ns = 0;
  67.         }
  68.         e = screen_buf[i];
  69.         if (IS_FA(e)) {
  70.             c = ' ';
  71.             fa = screen_buf[i];
  72.         }
  73.         if (FA_IS_ZERO(fa))
  74.             c = ' ';
  75.         else
  76.             c = cg2asc[e];
  77.         if (c == ' ')
  78.             ns++;
  79.         else {
  80.             any = True;
  81.             while (nr) {
  82.                 (void) fputc('\n', f);
  83.                 nr--;
  84.             }
  85.             while (ns) {
  86.                 (void) fputc(' ', f);
  87.                 ns--;
  88.             }
  89.             (void) fputc(c, f);
  90.         }
  91.     }
  92.     nr++;
  93.     if (!any && !even_if_empty)
  94.         return False;
  95.     while (nr) {
  96.         (void) fputc('\n', f);
  97.         nr--;
  98.     }
  99.     return True;
  100. }
  101.  
  102. #if defined(X3270_DISPLAY) /*[*/
  103.  
  104. /* Termination code for print text process. */
  105. static void
  106. print_text_done(FILE *f, Boolean do_popdown)
  107. {
  108.     int status;
  109.  
  110.     status = pclose(f);
  111.     if (status) {
  112.         popup_an_error("Print program exited with status %d.",
  113.             (status & 0xff00) > 8);
  114.     } else {
  115.         if (do_popdown)
  116.             XtPopdown(print_text_shell);
  117.         if (appres.do_confirms)
  118.             popup_an_info("Screen image printed.");
  119.     }
  120.  
  121. }
  122.  
  123. /* Callback for "OK" button on print text popup. */
  124. static void
  125. print_text_callback(Widget w unused, XtPointer client_data,
  126.     XtPointer call_data unused)
  127. {
  128.     char *filter;
  129.     FILE *f;
  130.  
  131.     filter = XawDialogGetValueString((Widget)client_data);
  132.     if (!filter) {
  133.         XtPopdown(print_text_shell);
  134.         return;
  135.     }
  136.     if (!(f = popen(filter, "w"))) {
  137.         popup_an_errno(errno, "popen(%s)", filter);
  138.         return;
  139.     }
  140.     (void) fprint_screen(f, True);
  141.     print_text_done(f, True);
  142. }
  143.  
  144. /* Print the contents of the screen as text. */
  145. void
  146. PrintText_action(Widget w unused, XEvent *event, String *params,
  147.     Cardinal *num_params)
  148. {
  149.     char *filter = get_resource(ResPrintTextCommand);
  150.     Boolean secure = appres.secure;
  151.  
  152.     action_debug(PrintText_action, event, params, num_params);
  153.     if (*num_params > 0)
  154.         filter = params[0];
  155.     if (*num_params > 1)
  156.         xs_warning("%s: extra arguments ignored",
  157.             action_name(PrintText_action));
  158.     if (filter == CN) {
  159.         xs_warning("%s: no %s defined",
  160.             action_name(PrintText_action), ResPrintTextCommand);
  161.         return;
  162.     }
  163.     if (filter[0] == '@') {
  164.         filter++;
  165.         secure = True;
  166.     }
  167.     if (secure) {
  168.         FILE *f;
  169.  
  170.         if (!(f = popen(filter, "w"))) {
  171.             popup_an_errno(errno, "popen(%s)", filter);
  172.             return;
  173.         }
  174.         (void) fprint_screen(f, True);
  175.         print_text_done(f, False);
  176.         return;
  177.     }
  178.     if (print_text_shell == NULL)
  179.         print_text_shell = create_form_popup("PrintText",
  180.             print_text_callback, (XtCallbackProc)NULL, FORM_AS_IS);
  181.     XtVaSetValues(XtNameToWidget(print_text_shell, ObjDialog),
  182.         XtNvalue, filter,
  183.         NULL);
  184.     popup_popup(print_text_shell, XtGrabExclusive);
  185. }
  186.  
  187. #if defined(X3270_MENUS) /*[*/
  188. /* Callback for Print Text menu option. */
  189. void
  190. print_text_option(Widget w, XtPointer client_data unused,
  191.     XtPointer call_data unused)
  192. {
  193.     Cardinal zero = 0;
  194.  
  195.     PrintText_action(w, (XEvent *)NULL, (String *)NULL, &zero);
  196. }
  197. #endif /*]*/
  198.  
  199.  
  200. /* Print Window popup */
  201.  
  202. /*
  203.  * Printing the window bitmap is a rather convoluted process:
  204.  *    The PrintWindow action calls PrintWindow_action(), or a menu option calls
  205.  *    print_window_option().
  206.  *    print_window_option() pops up the dialog.
  207.  *    The OK button on the dialog triggers print_window_callback.
  208.  *    print_window_callback pops down the dialog, then schedules a timeout
  209.  *     1 second away.
  210.  *    When the timeout expires, it triggers snap_it(), which finally calls
  211.  *     xwd.
  212.  * The timeout indirection is necessary because xwd prints the actual contents
  213.  * of the window, including any pop-up dialog in front of it.  We pop down the
  214.  * dialog, but then it is up to the server and Xt to send us the appropriate
  215.  * expose events to repaint our window.  Hopefully, one second is enough to do
  216.  * that.
  217.  */
  218.  
  219. /* Termination procedure for window print. */
  220. static void
  221. print_window_done(int status)
  222. {
  223.     if (status)
  224.         popup_an_error("Print program exited with status %d.",
  225.             (status & 0xff00) >> 8);
  226.     else if (appres.do_confirms)
  227.         popup_an_info("Bitmap printed.");
  228. }
  229.  
  230. /* Timeout callback for window print. */
  231. static void
  232. snap_it(XtPointer closure unused, XtIntervalId *id unused)
  233. {
  234.     if (!print_window_command)
  235.         return;
  236.     XSync(display, 0);
  237.     print_window_done(system(print_window_command));
  238.     print_window_command = CN;
  239. }
  240.  
  241. /* Callback for "OK" button on print window popup. */
  242. static void
  243. print_window_callback(Widget w unused, XtPointer client_data,
  244.     XtPointer call_data unused)
  245. {
  246.     print_window_command = XawDialogGetValueString((Widget)client_data);
  247.     XtPopdown(print_window_shell);
  248.     if (print_window_command)
  249.         (void) XtAppAddTimeOut(appcontext, 1000, snap_it, 0);
  250. }
  251.  
  252. /* Print the contents of the screen as a bitmap. */
  253. void
  254. PrintWindow_action(Widget w unused, XEvent *event, String *params,
  255.     Cardinal *num_params)
  256. {
  257.     char *filter = get_resource(ResPrintWindowCommand);
  258.     char *fb = XtMalloc(strlen(filter) + 16);
  259.     char *xfb = fb;
  260.     Boolean secure = appres.secure;
  261.  
  262.     action_debug(PrintWindow_action, event, params, num_params);
  263.     if (*num_params > 0)
  264.         filter = params[0];
  265.     if (*num_params > 1)
  266.         xs_warning("%s: extra arguments ignored",
  267.             action_name(PrintWindow_action));
  268.     if (filter == CN) {
  269.         xs_warning("%s: no %s defined",
  270.             action_name(PrintWindow_action), ResPrintWindowCommand);
  271.         return;
  272.     }
  273.     (void) sprintf(fb, filter, XtWindow(toplevel));
  274.     if (fb[0] == '@') {
  275.         secure = True;
  276.         xfb = fb + 1;
  277.     }
  278.     if (secure) {
  279.         print_window_done(system(xfb));
  280.         Free(fb);
  281.         return;
  282.     }
  283.     if (print_window_shell == NULL)
  284.         print_window_shell = create_form_popup("printWindow",
  285.             print_window_callback, (XtCallbackProc)NULL, FORM_AS_IS);
  286.     XtVaSetValues(XtNameToWidget(print_window_shell, ObjDialog),
  287.         XtNvalue, fb,
  288.         NULL);
  289.     popup_popup(print_window_shell, XtGrabExclusive);
  290. }
  291.  
  292. #if defined(X3270_MENUS) /*[*/
  293. /* Callback for menu Print Window option. */
  294. void
  295. print_window_option(Widget w, XtPointer client_data unused,
  296.     XtPointer call_data unused)
  297. {
  298.     Cardinal zero = 0;
  299.  
  300.     PrintWindow_action(w, (XEvent *)NULL, (String *)NULL, &zero);
  301. }
  302. #endif /*]*/
  303.  
  304. #endif /*]*/
  305.